package logic

import (
	"context"
	"errors"
	"houseRpc/models"

	"houseRpc/house"
	"houseRpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddHouseLogic {
	return &AddHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddHouseLogic) AddHouse(in *house.AddHouseRequest) (*house.AddHouseResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var u models.User
	var r models.RealName
	var h models.House
	u.ID = uint(in.UserId)
	if err = u.GetUserById(); err != nil {
		logx.Error("获取用户信息失败")
		return nil, errors.New("获取用户信息失败")
	}

	if err = r.GetRealNameByUserId(); err != nil {
		logx.Error("获取用户实名信息失败")
		return nil, errors.New("获取用户实名信息失败")
	}
	if r.ID == 0 {
		logx.Error("用户未实名")
		return nil, errors.New("用户未实名")
	}

	h.UserId = in.UserId
	h.Title = in.Title
	h.Ads = in.Ads
	h.Price = in.Price
	if err = h.CreateHouse(); err != nil {
		logx.Error("房源发布失败")
		return nil, errors.New("房源发布失败")
	}

	return &house.AddHouseResponse{
		Id: int64(h.ID),
	}, nil
}
